
<title>Update Order Details Page</title>
 

<?php 

/*

***************************************
*                                     *
* Copyright 2011 David Matthew Pugh   *
* Contact Info:thebravedave@gmail.com *
*                                     *
***************************************

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
   
require_once "../services/webservices.php";
require_once "index.php";
include('Mail.php');   
?>

<script type="text/javascript">
    function checkform()
    {   
        go=true;
        if(document.getElementById("order_status").value=='refunded')
        {
            var go = confirm("Are you sure you want to change the order status to refunded? Once an order's status has been changed to refunded, the actual money is refunded and the status can never be changed again.");
            return go;
            
        }   
    }
</script>
<?php


if(isset($_POST['order_id']))
{
    echo '<br /><br /><br /><br /><br />'; 
    $order_id = $_POST['order_id'];
?>
    <form action="./orderdetails.php" method="POST">
        <input type="hidden" name="order_id" value="<?php echo $order_id;?>" />
        <input type="submit" name="submit" value="go back to order details" />  
    </form>
<?php    
                   
    echo '<br /><br /><br />';
    if(isset($_POST['updatepackaging']))
    {    
        $email = $_POST['email'];   
        if(isset($_POST['submitupdateorderpackaging']))
        {
            $box_id = $_POST['box_id'];
            $status = $_POST['status'];
           
            $tracking_number = $_POST["trackingnumber"];
            $orderObject = new orders();
            $orderObject->order_box_id = $box_id;
            $orderObject->status = $status;
            $orderObject->updateorderboxstatusbyboxid();            
            $orderObject->tracking_number = $tracking_number;
            $result = $orderObject->updateorderboxtrackingnumberbyboxid();  
            
        }
        if(isset($_POST['extrapackagingemail']))
        {
           $tracking_number = $_POST['tracking_number'];
           $shippingObject = new shipping();
           $shippingObject->order_id = $order_id;
           $shippingReturn = $shippingObject->getshippingbyorderid();
           $shippingXml = new SimpleXMLElement($shippingReturn);
           foreach($shippingXml->Item as $shippingItem)
           {
               $service_id = $shippingItem->service_id;
               $shippingObject->service_id = $service_id;
               $shippingserviceReturn = $shippingObject->getshippingservicebyserviceid();
               $shippingserviceXml = new SimpleXMLElement($shippingserviceReturn);
               foreach($shippingserviceXml->Item as $shippingserviceItem)
               {
                   $shipping_service_name = $shippingserviceItem->service;
                   $carrier_id = $shippingserviceItem->carrier_id;
                   $shippingObject->carrier_id = $carrier_id;
                   $shippingcarrierReturn = $shippingObject->getshippingcarrierbycarrierid();
                   $shippingcarrierXml = new SimpleXMLElement($shippingcarrierReturn);
                   $shipping_carrier_name = $shippingcarrierXml->Item->carrier_name;
                   
               }
           } 
           $companynamearray = file('../configurational/companyname.php');        
           $companynamestring = "";
           foreach($companynamearray as $key => $value)
           {
                if(strpos($value, "companyname") !== false)
                {
                    $companynamestring = $value;
                }
           }
           $companynamearray = explode("=", $companynamestring);
           $companyname = trim($companynamearray[1]);   
           $emailstring = "";         
           $mailarray = file('../configurational/mailconfiguration.php');

           $usernamestring = "";

           foreach($mailarray as $key => $value)
           {
                
                if(strpos($value, "username") !== false)
                {
                    $usernamestring = $value;
                }

           }
           $mailusernamearray = explode("=", $usernamestring);
           $mailadmin = trim($mailusernamearray[1]);

           $emailstring .= "This is an email about your recent purchase from " . $companyname .".\n";
           $emailstring .= "Your package has shipped. \n";
           $emailstring .= "Below you will find the information related to your order \n\n\n";
           $emailstring .= "Order ID:" . $order_id . "\n";
           $emailstring .= "Shipment Method: " . $shipping_carrier_name . ", " . $shipping_service_name . "\n\n"; 
           if($tracking_number != "")
           {
                $emailstring .= "Shipment Tracking Number: " . $tracking_number . "\n";    
           }                       
               
           $emailstring .= "\n\nIf you have any questions about your order please contact " . $mailadmin . "\n";
           $emailstring .= "Please include your order number and we will get back to you as soon as possible.";

           
           
          
           $mailarray = file('../configurational/mailconfiguration.php');
            $mailserverstring = "";
            $portstring = "";
            $usernamestring = "";
            $passwordstring = "";
            foreach($mailarray as $key => $value)
            {
                if(strpos($value, "mailserver") !== false)
                {
                    $mailserverstring = $value;
                }
                if(strpos($value, "port") !== false)
                {
                   $portstring = $value; 
                }
                if(strpos($value, "username") !== false)
                {
                    $usernamestring = $value;
                }
                if(strpos($value, "password") !== false)
                {
                    $passwordstring = $value;
                }
            }
            $mailusernamearray = explode("=", $usernamestring);
            $username = trim($mailusernamearray[1]);
            $passwordarray = explode("=", $passwordstring);
            $password = trim($passwordarray[1]);
            $portarray = explode("=", $portstring);
            $port = trim($portarray[1]);
            $hostarray = explode("=", $mailserverstring);
            $host = trim($hostarray[1]); 
         
          

          
           $to = $email;
           $body = $emailstring; 
           $subject = "Your shipping information from " . $companyname; 
           $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
           $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));

           $mail = $smtp->send($to, $headers, $body);
           if($mail)
           {
               echo '<br /><b>Mail Information Sent</b><br /><br />';
           }
           else
           {
                echo '<br /><b>Mail information didnt sent correctly. Check your mail configuration data in mailconfiguration.php</b>';     
           } 
          
            
        }
        if(isset($_POST['sendorderpackagingemail']))
        {  
           $shippingObject = new shipping();
           $shippingObject->order_id = $order_id;
           $shippingReturn = $shippingObject->getshippingbyorderid();
           $shippingXml = new SimpleXMLElement($shippingReturn);
           foreach($shippingXml->Item as $shippingItem)
           {
               $carrier = $shippingItem->carrier;
               $service = $shippingItem->service;
              
               $shippingserviceReturn = $shippingObject->getshippingservicebyserviceid();
               $shippingserviceXml = new SimpleXMLElement($shippingserviceReturn);
              
           }
                     
           $box_id = $_POST['box_id'];
           $trackingnumber = $_POST['trackingnumber'];
           $status = $_POST['status'];
           $numBoxes = $_POST['numboxes'];
           $orderObject = new orders();
           $orderObject->order_box_id = $box_id;
           $orderObject->mail = "sent";
           $orderObject->updateorderboxmailstatusbyboxid();
           
           $companynamearray = file('../configurational/companyname.php');        
           $companynamestring = "";
           foreach($companynamearray as $key => $value)
           {
                if(strpos($value, "companyname") !== false)
                {
                    $companynamestring = $value;
                }
           }
           $companynamearray = explode("=", $companynamestring);
           $companyname = trim($companynamearray[1]);   
           $emailstring = "";
           $shipmentnum = 1; 
           
                       $mailarray = file('../configurational/mailconfiguration.php');
            $mailserverstring = "";
            $portstring = "";
            $usernamestring = "";
            $passwordstring = "";
            foreach($mailarray as $key => $value)
            {
                if(strpos($value, "mailserver") !== false)
                {
                    $mailserverstring = $value;
                }
                if(strpos($value, "port") !== false)
                {
                   $portstring = $value; 
                }
                if(strpos($value, "username") !== false)
                {
                    $usernamestring = $value;
                }
                if(strpos($value, "password") !== false)
                {
                    $passwordstring = $value;
                }
            }
            $mailusernamearray = explode("=", $usernamestring);
            $mailadmin = trim($mailusernamearray[1]);


          
           $emailstring .= "This is an email about your recent purchase from " . $companyname .".\n";
           if($numBoxes > 1)
           {
              $emailstring .= "One of your packages has shipped. \n"; 
           }
           else
           {
               $emailstring .= "Your package has shipped. \n";      
           }
           
           $emailstring .= "Below you will find the information related to your order \n\n\n";
           $emailstring .= "Order ID:" . $order_id . "\n";
           $emailstring .= "Shipment Method: " . $carrier . ", " . $service . "\n\n";  
           if($trackingnumber != "")
           {
               $emailstring .="Shipment Tracking Number: " . $trackingnumber . "\n"; 
           }   
              
           $emailstring .= "\n\nIf you have any questions about your order please contact " . $mailadmin . "\n";
           $emailstring .= "Please include your order number and we will get back to you as soon as possible.";
                 
           
           $mailarray = file('../configurational/mailconfiguration.php');
            $mailserverstring = "";
            $portstring = "";
            $usernamestring = "";
            $passwordstring = "";
            foreach($mailarray as $key => $value)
            {
                if(strpos($value, "mailserver") !== false)
                {
                    $mailserverstring = $value;
                }
                if(strpos($value, "port") !== false)
                {
                   $portstring = $value; 
                }
                if(strpos($value, "username") !== false)
                {
                    $usernamestring = $value;
                }
                if(strpos($value, "password") !== false)
                {
                    $passwordstring = $value;
                }
            }
           $mailusernamearray = explode("=", $usernamestring);
           $username = trim($mailusernamearray[1]);
           $passwordarray = explode("=", $passwordstring);
           $password = trim($passwordarray[1]);
           $portarray = explode("=", $portstring);
           $port = trim($portarray[1]);
           $hostarray = explode("=", $mailserverstring);
           $host = trim($hostarray[1]); 

           $from = $mailadmin;     
           
          
          
           $to = $email;
           $body = $emailstring; 
           $subject = "Your shipping information from " . $companyname; 
           $headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
           $smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));

           $mail = $smtp->send($to, $headers, $body);
           if($mail)
           {
               echo '<br /><b>Mail Information Sent</b><br /><br />';
           }
           else
           {
                echo '<br /><b>Mail information didnt sent correctly. Check your mail configuration data in mailconfiguration.php</b>';     
           }
        }
        
        
        
        
        $getRequest = new orders();                
        $getRequest->order_id = $order_id;
        $getResponse = $getRequest->getorderbyorderid();

       
        $xml = new SimpleXMLElement($getResponse);
        $item_count = $xml->Item_Count;                  
        if($item_count > 0)
        {

            $items_id_name_array = array(); 
            foreach ($xml->Item as $eachitem3)         
            {       
                $order_id = $eachitem3->order_id;  
                $gw_id = (int)$eachitem3->gw_id;                 
                $sw_id = (string)$eachitem3->sw_id;
                $name = $eachitem3->name;                 
                 
                $getrequest2 = new item();
                $getrequest2->gw_id = $gw_id;
                $getresponse2 = $getrequest2->getitembyid();
                $xml = new SimpleXMLElement($getresponse2);
                foreach($xml->Item as $eachitem2)
                {
                    $category_id = $eachitem2->category_id;
                               
                }
                $getrequest3 = new itemcategory();
                $getrequest3->category_id = $category_id;
                $getresponse3 = $getrequest3->getcategorybycategoryid();                    
                $xml3 = new SimpleXMLElement($getresponse3);
                foreach($xml3->Item as $eachitem3)
                {
                    $category = $eachitem3->category;
                    $parent_category_id = $eachitem3->parent_category_id;
                    $parent_category_id = (int)$parent_category_id;
                            
                }           
                $getrequest4 = new itemcategory();
                $getrequest4->category_id = $parent_category_id;
                $getresponse4 = $getrequest3->getcategorybycategoryid();
                $xml4 = new SimpleXMLElement($getresponse4);
                foreach($xml4->Item as $eachitem4)
                {
                    $parent_category = $eachitem4->category;   
                }           
                if($sw_id != "")
                {                     
                    $namedetails = $name->xml->name;                  
                    $sw_name = $namedetails->sw_name; 
                    $gw_name = $namedetails->gw_name;                    
                    $descriptor_items = $namedetails->descriptor_item;
                    $num_desc_items = count($descriptor_items);                
                    $temp_num = 0;
                    $string = $gw_name . "[";                 
                    while($temp_num != $num_desc_items)
                    {                     
                        $is_category_array = $descriptor_items->$temp_num;
                        $is_category = $is_category_array->category;
                        $is_descriptor = $is_category_array->descriptor;
                        $temp_num++;
                        $string .= $is_category . ":" . $is_descriptor . ", ";
                    }
                    $string = rtrim($string, ", ") . "]"; 
                    $items_id_name_array[$sw_id] = $string;                
                    
                }
                else
                {
                    $items_id_name_array[$gw_id] = (string)$name;
                   
                }                        
            }                 
        }   
        $orderObject = new orders();
        $orderObject->order_id = $order_id;
        $orderResponse = $orderObject->getorderboxesbyorderid();
        $orderXml = new SimpleXMLElement($orderResponse);
        $numBoxes = $orderXml->Item_Count;
        
        echo '<b><big>Packaging For Shipping</big></b><br /><br />';
        echo '<div class="smallcore">';

        foreach($orderXml->Item as $orderItem)
        {   
            echo '<div class="smallcorerightmargin">';
            echo '<form action="./updateorderdetails.php" method="POST" >'; 
            echo '<input type="hidden" name="order_id" value="' . $order_id . '"/>';
            echo '<input type="hidden" name="updatepackaging" value="updatepackaging"/>';          
            
            $order_box_id = $orderItem->id;
            $status = $orderItem->status;
            $mailstatus = $orderItem->mail;
            $tracking_number = $orderItem->tracking_number;
            $orderObject->order_box_id = $order_box_id;
            $orderitemsResponse = $orderObject->getorderboxitemsbyboxid();                      
            $orderitemsXml = new SimpleXMLElement($orderitemsResponse);
            echo '<br /><b>Box #' . $order_box_id . '</b><br />';
            echo '<input type="hidden" name="box_id" value="' . $order_box_id . '" />';
            echo '<input type="hidden" name="email" value="' . $email . '" />';
            echo '<input type="hidden" name="numboxes" value="' . $numBoxes . '" />';   
            echo '<b>Status:</b> <select name="status">'; 
            echo '<option value="' . $status . '">' . $status . '</option>';
            echo '<option value="not shipped">not shipped</option>';   
            echo '<option value="shipped">shipped</option>'; 
            echo '</select><br />';
            echo '<b>Email Status: </b>' . $mailstatus;
            echo '<b><br />Tracking #</b><input type="text" name="trackingnumber" value="' . $tracking_number . '" /><br /><br />';
            
            $tmpnum = 1;
            foreach($orderitemsXml->Item as $orderitemsItem)
            {
                $orderitem_id = $orderitemsItem->id;
                $orderitem_item_id = $orderitemsItem->item_id;
               
                if(strpos($orderitem_item_id, 'SWIDGET' == false)) //then its a general widget      
                {
                    echo '<b>Item #</b> ' . $tmpnum . '</b><br />';
                    echo '<b>Item ID:</b>' . $orderitem_item_id . '<br />';
                    echo '<b>Item Name:</b>' . $items_id_name_array[(int)$orderitem_item_id] . '<br /><br />';   
                }
                else   //then its a specific widget         
                {
                    echo '<b>Item #' . $tmpnum . '</b><br />';
                    echo '<b>Item ID:</b>' . $orderitem_item_id . '<br />';
                    echo '<b>Name:</b>' . $items_id_name_array[(string)$orderitem_item_id] . '<br /><br />'; 
                    
                    
                }
                    $tmpnum++;
             
            }
            echo '<input type="submit" name="submitupdateorderpackaging" value="Update Box #' . $order_box_id . '" />';
            if($status == "shipped"  && $mailstatus != "sent")
            {
                 echo '<input type="submit" name="sendorderpackagingemail" value="Send Box #' . $order_box_id . ' Shipped Email" />';             
            }
            echo '</form>';
            echo '</div>';
            echo '<br />';
           
        }  
         echo '</div>';                                       

        echo '<b>Extra Packaging Email Section</b><br />';
        echo '<div class="smallcorerightmargin">';
        echo '<form action="./updateorderdetails.php" method="POST" >';
        echo '<br /><b>Tracking Number:</b><input type="text" name="tracking_number" />';
        echo '<input type="hidden" name="order_id" value="' . $order_id . '"/>';
        echo '<input type="hidden" name="updatepackaging" value="updatepackaging"/>'; 
        echo '<input type="hidden" name="email" value="' . $email . '"/><br />';
        echo '<input type="submit" name="extrapackagingemail" value="send extra packaging email"/>';
        echo '</div>';
        
    } 
    
    
    if(isset($_POST['order_status']))
    {
        $order_status = $_POST['order_status'];       
        if(isset($_POST['updateorderstatus']))
        {
            $order_status = $_POST['order_status'];
            if($order_status == "refunded")
            {
                //must do service call to paypal and do a refund for this order.
                $pphttpp = new paypalhttppost();
                $httpParsedResponseAr = $pphttpp->FullyRefundTransaction($order_id);
               
               echo '<br /><br />'; 
                if("Success" == $httpParsedResponseAr["ACK"]) 
                {
                    echo "<b>Order Refunded</b><br />";
                }
                else
                {
                    echo "There was an error refunding the order.<br />";
                    $error = urldecode($httpParsedResponseAr['L_LONGMESSAGE0']);
                    echo "PayPal Error Reason: " . $error . '<br />';
                    echo "More information can be found in your Paypal merchant account<br />";
                } 
                
                //set the database payment table order_status to the current POST'ed order status  
                $paymentObject = new payment();
                $paymentObject->order_id = $order_id;
                $paymentObject->order_status = $order_status;
                $paymentObject->updatepaymentorderstatus();
            }
            else
            {
                //set the database payment table order_status to the current POST'ed order status
                $paymentObject = new payment();
                $paymentObject->order_id = $order_id;
                $paymentObject->order_status = $order_status;
                $paymentObject->updatepaymentorderstatus();
            }
            
        }
        
        
        if($_POST['order_status'] != "refunded")
        {
?>          <div class="leftfloatbar">
            <b>Choose Order Status Update</b><br /><br />
            <form action="./updateorderdetails.php" method="POST">
                <select id="order_status" name="order_status">
                    <option value="<?php echo $order_status; ?>"><?php echo $order_status; ?></option>
                    <option value="incomplete">incomplete</option>
                    <option value="complete">complete</option>
                    <option value="refunded">refunded</option>
                </select>
                <br />
                <input type="hidden" name="order_id" value="<?php echo $order_id; ?>" />
                <input type="hidden" name="updateorderstatus" value="updateorderstatus" /><br />  
                <input type="submit" id="submitorderdetailsupdate" name="submitorderdetailsupdate" value="update order details" onclick="javascript: return checkform();"/>
            </form>      
            </div>
            <br /><br /><br /><br /><br /><br />
            <br /><br />
<?php                
        }
        else
        {
            echo '<b>The order status has been set to refunded and the payment was refunded successfully.</b>';        
                
                       
                
                
                
        }
    }
    
}  
  
?>
